package io.itit.ecp.admin.provide.action.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.resp.OptimizationSuggestionResp;
import io.itit.ecp.admin.server.entity.OptimizationSuggestionEntity;
import io.itit.ecp.admin.server.service.IOptimizationSuggestionService;
import io.itit.grass.common.provide.action.req.BaseBatchIdReq;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.req.BasePageReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IAdd;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.action.validation.IRequired;
import io.itit.grass.common.provide.action.validation.IUpdate;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 优化建议管理控制类
 */
@Api(tags = "优化建议管理")
@RestController
@RequestMapping("/ecp-admin/optimizationSuggestion")
public class OptimizationSuggestionController {

    @Autowired
    private IOptimizationSuggestionService optimizationSuggestionService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions("ecp-admin:optimizationSuggestion:search")
    public ResultResp<IPage<OptimizationSuggestionResp>> queryPage(@RequestBody @Validated(IQueryPage.class) BasePageReq req) {
        Page<OptimizationSuggestionResp> page = buildQW(req).joinPage(PageUtils.getPage(req, OptimizationSuggestionResp.class), OptimizationSuggestionResp.class);
        return ResultResp.success(page);
    }

    @ApiOperation("新增")
    @PostMapping("add")
    @RequiresPermissions("ecp-admin:optimizationSuggestion:add")
    public ResultResp<Boolean> add(@RequestBody @Validated({IAdd.class}) OptimizationSuggestionEntity req) {
        return ResultResp.success(optimizationSuggestionService.save(req));
    }

    @ApiOperation("获取内容")
    @GetMapping("getcontent")
    public ResultResp<String> get(OptimizationSuggestionEntity optimizationSuggestion) {
        List<OptimizationSuggestionEntity> suggestionEntities = optimizationSuggestionService.lambdaQuery()
                .select(OptimizationSuggestionEntity::getId, OptimizationSuggestionEntity::getContent)
                .eq(StringUtils.hasLength(optimizationSuggestion.getId()), OptimizationSuggestionEntity::getId, optimizationSuggestion.getId())
                .eq(StringUtils.hasLength(optimizationSuggestion.getName()), OptimizationSuggestionEntity::getName, optimizationSuggestion.getName())
                .eq(StringUtils.hasLength(optimizationSuggestion.getKey()), OptimizationSuggestionEntity::getKey, optimizationSuggestion.getKey())
                .ge(Objects.nonNull(optimizationSuggestion.getMinValue()), OptimizationSuggestionEntity::getMinValue, optimizationSuggestion.getMinValue())
                .le(Objects.nonNull(optimizationSuggestion.getMaxValue()), OptimizationSuggestionEntity::getMaxValue, optimizationSuggestion.getMaxValue())
                .orderByDesc(OptimizationSuggestionEntity::getId).list();
        return ResultResp.success(CollectionUtils.isEmpty(suggestionEntities) ? null : suggestionEntities.get(0).getContent());
    }

    @ApiOperation("编辑")
    @PostMapping("update")
    @RequiresPermissions("ecp-admin:optimizationSuggestion:update")
    public ResultResp<Boolean> update(@RequestBody @Validated({IUpdate.class}) OptimizationSuggestionEntity req) {
        return ResultResp.success(optimizationSuggestionService.updateById(req));
    }

    @ApiOperation("删除(根据Id删除)")
    @PostMapping("deleteById")
    @RequiresPermissions("ecp-admin:optimizationSuggestion:deleteById")
    public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
        return ResultResp.success(optimizationSuggestionService.removeById(req.getId()));
    }

    @ApiOperation("批量删除(根据Id批量删除)")
    @PostMapping("batchDelete")
    @RequiresPermissions("ecp-admin:optimizationSuggestion:batchDelete")
    public ResultResp<Boolean> batchDelete(@RequestBody @Validated({IRequired.class}) BaseBatchIdReq req) {
        return ResultResp.success(optimizationSuggestionService.removeBatchByIds(req.getIds()));
    }

    private static JoinLambdaWrapper<OptimizationSuggestionEntity> buildQW(BasePageReq req) {
        return Joins.of(OptimizationSuggestionEntity.class)
                .orderByDesc(StringUtils.hasLength(req.getSortColumn()), OptimizationSuggestionEntity::getCreateTm);
    }
}
